<template>
  <div class="dialog">
    <el-dialog
      :title="titleText || '添加作业任务'"
      :visible.sync="showDialog"
      width="65%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        v-if="processBatchType === '自主生产'"
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="批次名称：" prop="processBatchName">
              <el-input
                v-model="formData.processBatchName"
                placeholder="请输入批次名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次号：" prop="processBatchNo">
              <el-input
                v-model="formData.processBatchNo"
                :disabled="true"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="加工产品：" prop="productMaterialNo">
              <el-select
                v-model="formData.productMaterialNo"
                style="width: 100%;"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in productionOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原料名称：" prop="rawMaterialNo">
              <el-select
                v-model="formData.rawMaterialNo"
                style="width: 100%;"
                placeholder="请选择"
                clearable
                @change="rawMaterialChange"
              >
                <el-option
                  v-for="item in rawMaterialOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="原料批次：" prop="materialBatchNo">
              <el-select
                v-model="formData.materialBatchNo"
                style="width: 100%;"
                placeholder="请选择"
                clearable
                @change="materialBatchChange"
              >
                <el-option
                  v-for="item in rawMaterialBatchOptions"
                  :key="item.rawMaterialBatchNo"
                  :label="item.rawMaterialBatchNo"
                  :value="item.rawMaterialBatchNo"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col style="display: flex;" :span="12">
            <el-form-item
              style="width: 75%;"
              label="原料数量："
              prop="rawMaterialQuantity"
            >
              <el-input
                v-model="formData.rawMaterialQuantity"
                :placeholder="inputRawMaterialQuantity"
                clearable
              />
            </el-form-item>
            <el-form-item style="width: 25%;">
              <el-select
                v-model="formData.rawMaterialUnitNo"
                placeholder="单位"
                disabled
              >
                <el-option
                  v-for="item in UnitOptions"
                  :key="item.unitNo"
                  :label="item.unitName"
                  :value="item.unitNo"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="加工产线：" prop="productionLineNo">
              <el-select
                v-model="formData.productionLineNo"
                style="width: 100%;"
                placeholder="请选择"
                clearable
                @change="productionLineChange"
              >
                <el-option
                  v-for="item in productionLineOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <div
          v-for="(item,idx) in formData.processingTaskList"
          :key="idx"
          style="width: 100%;"
        >
          <el-row v-if="formData.productionLineNo" :gutter="24">
            <el-col :span="12">
              <el-form-item label="工序名称：">
                <el-row type="flex" justify="space-between">
                  <el-col :span="17">
                    <el-input
                      v-model="item.productionProcessesName"
                      :disabled="true"
                    />
                  </el-col>
                  <el-col :span="7" style="text-align: right;">
                    <el-button type="danger" @click="del(idx)">删除此工序</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="加工作业名称：">
                <el-input
                  v-model="item.processingTaskName"
                  placeholder="请输入加工作业名称"
                  clearable
                  disabled
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="formData.productionLineNo" :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="加工开始时间："
                :prop="'processingTaskList.' + idx + '.startTime'"
                :rules="[{ required: true, message: '开始时间不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-date-picker
                  v-model="item.startTime"
                  style="width: 100%;"
                  type="datetime"
                  :picker-options="selectStartDate(formData.processingTaskList[idx].endTime)"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择时间"
                  @change="getTimeChange(idx,item,'in')"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="加工结束时间："
                :prop="'processingTaskList.' + idx + '.endTime'"
                :rules="[{ required: true, message: '结束时间不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-date-picker
                  v-model="item.endTime"
                  style="width: 100%;"
                  type="datetime"
                  :picker-options="selectEndDate(formData.processingTaskList[idx].startTime)"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择时间"
                  @change="getTimeChange(idx,item,'in')"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <div
            v-if="formData.productionLineNo"
            class="allGongyi"
            style="width: 100%;"
          >
            <div
              v-for="taskitem,index in item.taskCraftList"
              :key="index"
              style="width: 50%;"
            >
              <el-form-item
                class="gongyi"
                :label="'工艺参数' + (index + 1) + ':'"
                :prop="'processingTaskList.' + idx + '.taskCraftList.' + index + '.value'"
                :rules="[{ required: true, message: '工艺参数值不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-input
                  v-model="taskitem.craftName"
                  style="width: 40%;"
                  disabled
                  placeholder="请输入工艺参数名称"
                />
                <el-input
                  v-model="taskitem.value"
                  style="width: 27%;"
                  :disabled="!!taskitem.dataItemId"
                  placeholder="工艺参数值"
                />
                <el-input
                  v-model="taskitem.unitName"
                  style="width: 16%;"
                  :disabled="!!taskitem.dataItemId"
                  placeholder="单位"
                />
                <el-button
                  style="width: 16%;"
                  class="del"
                  type="danger"
                  @click="delTaskgy(idx,index)"
                >删除</el-button>
              </el-form-item>
            </div>
          </div>
          <el-row v-if="item.productionLineNo" :gutter="24">
            <el-col :span="12">
              <div class="addP">
                <el-button
                  type="primary"
                  @click="addTaskgy(idx,item.productionProcessesNo)"
                >添加新的工艺参数</el-button>
              </div>
            </el-col>
          </el-row>
          <el-row v-if="item.productionLineNo" :gutter="24">
            <el-col :span="12">
              <el-form-item label="作业人：">
                <el-input
                  v-model="item.personName"
                  disabled
                  placeholder="请输入作业人"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <el-form
        v-if="processBatchType === '委托加工'"
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="委托单位名称：" prop="manufacturingName">
              <el-select
                v-model="formData.manufacturingName"
                style="width: 100%;"
                placeholder="请输入批次名称"
                clearable
              >
                <el-option
                  v-for="item in manufacturingList"
                  :key="item.manufacturingNo"
                  :label="item.manufacturingName"
                  :value="item.manufacturingName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加工产品：" prop="productMaterialNo">
              <el-select
                v-model="formData.productMaterialNo"
                style="width: 100%;"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in productionOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="原料名称：" prop="rawMaterialNo">
              <el-select
                v-model="formData.rawMaterialNo"
                style="width: 100%;"
                placeholder="请选择"
                clearable
                @change="rawMaterialChange"
              >
                <el-option
                  v-for="item in rawMaterialOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="rawMaterialQuantity">
            <el-form-item
              style="width: 75%;"
              label="原料数量："
              prop="rawMaterialQuantity"
            >
              <el-input
                v-model="formData.rawMaterialQuantity"
                placeholder="请输入原料数量"
                clearable
              />
            </el-form-item>
            <el-form-item style="width: 25%;" prop="rawMaterialUnitNo">
              <el-select
                v-model="formData.rawMaterialUnitNo"
                placeholder="单位"
                clearable
                @change="getUnit"
              >
                <el-option
                  v-for="item in UnitOptions"
                  :key="item.unitNo"
                  :label="item.unitName"
                  :value="item.unitNo"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item
              label="加工开始时间："
              prop="startTime"
              :rules="[{ required: true, message: '开始时间不能为空', trigger: ['change', 'blur'] }]"
            >
              <el-date-picker
                v-model="formData.startTime"
                style="width: 100%;"
                :picker-options="selectStartDate(formData.endTime)"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择时间"
                @change="getTimeChange('none',formData,'out')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="加工结束时间："
              prop="endTime"
              :rules="[{ required: true, message: '结束时间不能为空', trigger: ['change', 'blur'] }]"
            >
              <el-date-picker
                v-model="formData.endTime"
                style="width: 100%;"
                :picker-options="selectEndDate(formData.startTime)"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择时间"
                @change="getTimeChange('none',formData,'out')"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="加工产线：" prop="productionLineNo">
              <el-select
                v-model="formData.productionLineNo"
                style="width: 100%;"
                placeholder="请选择"
                clearable
                @change="productionLineChange"
              >
                <el-option
                  v-for="item in productionLineOptions"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <div
          v-for="(item,idx) in formData.processingTaskList"
          :key="idx"
          style="width: 100%;"
        >
          <el-row v-if="formData.productionLineNo" :gutter="24">
            <el-col :span="12">
              <el-form-item label="工序名称：">
                <el-row type="flex" justify="space-between">
                  <el-col :span="17">
                    <el-input
                      v-model="item.productionProcessesName"
                      :disabled="true"
                    />
                  </el-col>
                  <el-col :span="7" style="text-align: right;">
                    <el-button type="danger" @click="del(idx)">删除此工序</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="加工作业名称：">
                <el-input
                  v-model="item.processingTaskName"
                  placeholder="请输入加工作业名称"
                  clearable
                  disabled
                />
              </el-form-item>
            </el-col>
          </el-row>
          <div
            v-if="formData.productionLineNo"
            class="allGongyi"
            style="width: 100%;"
          >
            <div
              v-for="taskitem,index in item.taskCraftList"
              :key="index"
              style="width: 50%;"
            >
              <el-form-item
                class="gongyi"
                :label="'工艺参数' + (index + 1) + ':'"
                :prop="'processingTaskList.' + idx + '.taskCraftList.' + index + '.value'"
                :rules="[{ required: true, message: '工艺参数值不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-input
                  v-model="taskitem.craftName"
                  style="width: 38%;"
                  disabled
                  placeholder="请输入工艺参数名称"
                />
                <el-input
                  v-model="taskitem.value"
                  style="width: 28%;"
                  :disabled="!!taskitem.dataItemId"
                  placeholder="工艺参数值"
                />
                <el-input
                  v-model="taskitem.unitName"
                  style="width: 16%;"
                  :disabled="!!taskitem.dataItemId"
                  placeholder="单位"
                />
                <el-button
                  style="width: 17%;"
                  class="0"
                  type="danger"
                  @click="delTaskgy(idx,index)"
                >删除</el-button>
              </el-form-item>
            </div>
          </div>
          <el-row v-if="item.productionLineNo" :gutter="24">
            <el-col :span="12">
              <div class="addP">
                <el-button
                  type="primary"
                  @click="addTaskgy(idx,item.productionProcessesNo)"
                >添加新的工艺参数</el-button>
              </div>
            </el-col>
          </el-row>
          <el-row v-if="item.productionLineNo" :gutter="24">
            <el-col :span="12">
              <el-form-item label="作业人：">
                <el-input
                  v-model="item.personName"
                  disabled
                  placeholder="请输入作业人"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="加工产品等级：">
              <el-input
                v-model="formData.grade"
                style="width: 100%;"
                placeholder="选择加工产品等级"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="加工量："
              prop="output"
              :rules="[{ required: true, message: '加工量不能为空', trigger: ['change', 'blur'] },
                       {pattern:/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,message:'数量应为大于0的数字',trigger: ['change', 'blur']}]"
            >
              <el-input
                v-model="formData.output"
                style="width: 100%;"
                placeholder="请输入加工量"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
    <AddProcessParameters
      :show="addProcessDialog"
      :production-processes-no="productionProcessesNo"
      :unit-options="UnitOptions"
      @closeDialog="closeAddProcessDialog"
      @addProcessP="addProcessP"
    />
  </div>
</template>
<script>
// import { deepClone } from "@/utils/index";
import AddProcessParameters from "./add-process-parameters.vue";
import productionLineApi from "@/api/processing-management/basic-info-manage/production-line-manage/index";
import UnitApi from "@/api/processing-management/basic-info-manage/data-item-manage/index";
export default {
  name: "Addeditdialog",
  components: { AddProcessParameters },
  props: {
    manufacturingList: {
      type: Array,
      default: () => []
    },
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    processBatchNo: {
      type: String,
      default: ""
    },
    processBatchType: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    productionOptions: {
      type: Array,
      default: () => []
    },
    productionLineOptions: {
      type: Array,
      default: () => []
    },
    rawMaterialOptions: {
      type: Array,
      default: () => []
    },
    rawMaterialBatchOptions: {
      type: Array,
      default: () => []
    },
    formDataCopy: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      inputRawMaterialQuantity: "请输入原料数量",
      gongyiIndex: 0,
      UnitOptions: [],
      productionProcessesNo: "",
      addProcessDialog: false,
      // 加工时间
      timeList: [],
      showDialog: this.show,
      // 展示
      formData: {
        processBatchNo: "",
        materialBatchNo: "",
        processingTaskList: [
          {
            taskCraftList: []
          }
        ],
        ...this.formDataCopy
      },
      taskCraftList: [],
      sourceOptions: [
        {
          value: 0,
          label: "设备数据采集"
        },
        {
          value: 1,
          label: "人工手动录入"
        }
      ],
      rules: {
        processBatchName: [
          {
            required: true,
            message: "批次名称不能为空",
            trigger: ["change", "blur"]
          }
        ],
        productMaterialNo: [
          {
            required: true,
            message: "加工产品不能为空",
            trigger: ["change", "blur"]
          }
        ],
        estimatedProcessingQuantity: [
          {
            required: false,
            message: "估算加工量不能为空",
            trigger: ["change", "blur"]
          }
        ],
        rawMaterialNo: [
          {
            required: true,
            message: "原料名称不能为空",
            trigger: ["change", "blur"]
          }
        ],
        materialBatchNo: [
          {
            required: true,
            message: "批原料批次不能为空",
            trigger: ["change", "blur"]
          }
        ],
        rawMaterialQuantity: [
          {
            required: true,
            message: "原料数量不能为空",
            trigger: ["change", "blur"]
          },
          {
            pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,
            message: "数量应为大于0的数字",
            trigger: ["change", "blur"]
          }
        ],
        productionLineNo: [
          {
            required: true,
            message: "加工产线不能为空",
            trigger: ["change", "blur"]
          }
        ],
        startTime: [
          {
            required: true,
            message: "开始时间不能为空",
            trigger: ["change", "blur"]
          }
        ],
        endTime: [
          {
            required: true,
            message: "结束时间不能为空",
            trigger: ["change", "blur"]
          }
        ],
        output: [
          {
            required: true,
            message: "加工量不能为空",
            trigger: ["change", "blur"]
          }
        ],
        manufacturingName: [
          {
            required: true,
            message: "加工单位不能为空",
            trigger: ["change", "blur"]
          }
        ],
        rawMaterialUnitNo: [
          {
            required: true,
            message: "单位不能为空",
            trigger: ["change", "blur"]
          }
        ],
        processBatchNo: [
          {
            required: true,
            message: "批次号不能为空",
            trigger: ["change", "blur"]
          }
        ]
      }
    };
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    formDataCopy(v) {
      this.formData = { ...this.formData, ...v };
    },
    processBatchNo(v) {
      this.$set(this.formData, "processBatchNo", v);
    }
  },
  created() {},
  mounted() {
    this.getUnitOptions();
  },
  methods: {
    // 限制前面日期不能低于后面日期
    selectStartDate(endTime) {
      // const self = this;
      return {
        disabledDate(time) {
          if (endTime) {
            //  如果结束时间不为空，则小于结束时间
            return new Date(endTime).getTime() < time.getTime();
          } else {
            // return time.getTime() > Date.now(); // 开始时间不选时，结束时间最大值小于等于当天
          }
        }
      };
    },
    selectEndDate(startTime) {
      // const self = this;
      return {
        disabledDate(time) {
          if (startTime) {
            //  如果开始时间不为空，则结束时间大于开始时间
            self.time = new Date();
            return (
              new Date(startTime).getTime() - 24 * 60 * 60 * 1000 >
              time.getTime()
            );
          } else {
            // return time.getTime() > Date.now(); // 开始时间不选时，结束时间最大值小于等于当天
          }
        }
      };
    },
    getTimeChange(index, item, type) {
      if (type === "in") {
        if (item.startTime && item.endTime) {
          item.taskCraftList.forEach((element, idx) => {
            if (element.dataItemId) {
              let a = item.startTime.replace(/-/g, "/");
              let b = item.endTime.replace(/-/g, "/");
              productionLineApi
                .getAvgByDataItemIdAndDate({
                  dataItemId: element.dataItemId,
                  startDate: a,
                  endDate: b
                })
                .then(res => {
                  console.log(res);
                  this.$set(
                    this.formData.processingTaskList[index].taskCraftList[idx],
                    "value",
                    res.data.avg
                  );
                  this.$set(
                    this.formData.processingTaskList[index].taskCraftList[idx],
                    "unitName",
                    res.data.unit
                  );
                  console.log(this.formData, "b");
                });
            } else {
              //
            }
          });
        }
      }
      if (type === "out") {
        console.log(this.formData, "out");
        if (
          this.formData.startTime &&
          this.formData.endTime &&
          this.formData.processingTaskList[0]?.productionProcessesNo
        ) {
          this.formData.processingTaskList.forEach((item1, idx1) => {
            item1.taskCraftList.forEach((element, idx2) => {
              if (element.dataItemId) {
                let a = this.formData.startTime.replace(/-/g, "/");
                let b = this.formData.endTime.replace(/-/g, "/");
                productionLineApi
                  .getAvgByDataItemIdAndDate({
                    dataItemId: element.dataItemId,
                    startDate: a,
                    endDate: b
                  })
                  .then(res => {
                    console.log(res);
                    this.$set(
                      this.formData.processingTaskList[idx1].taskCraftList[
                        idx2
                      ],
                      "value",
                      res.data.avg
                    );
                    this.$set(
                      this.formData.processingTaskList[idx1].taskCraftList[
                        idx2
                      ],
                      "unitName",
                      res.data.unit
                    );
                  });
              } else {
                //
              }
            });
          });
        }
      }
    },
    async productionLineChange(item) {
      this.$set(this.formData, "processingTaskList", []);
      await productionLineApi.queryDetailByNo(item).then(res => {
        if (res.code === 1) {
          let data = [];
          res.data.forEach(item => {
            if (item.jobTemplate) {
              let element = {
                ...item,
                personNo: item.jobTemplate.personNo,
                personName: item.jobTemplate.personName,
                processingTaskNo: item.jobTemplate.jobTemplateNo,
                processingTaskName: item.jobTemplate.jobTemplateName,
                taskCraftList: item.jobTemplate?.craftList
              };
              data.push(element);
            }
          });
          this.$set(this.formData, "processingTaskList", data);
        }
      });
      await this.$nextTick(this.getTimeChange("none", this.formData, "out"));
    },
    addProcessP(e) {
      if (this.processBatchType === "自主生产") {
        if (
          this.formData.processingTaskList[this.gongyiIndex].startTime &&
          this.formData.processingTaskList[this.gongyiIndex].endTime
        ) {
          e.forEach(element => {
            if (element.dataItemId) {
              let a = this.formData.processingTaskList[
                this.gongyiIndex
              ].startTime.replace(/-/g, "/");
              let b = this.formData.processingTaskList[
                this.gongyiIndex
              ].endTime.replace(/-/g, "/");
              productionLineApi
                .getAvgByDataItemIdAndDate({
                  dataItemId: element.dataItemId,
                  startDate: a,
                  endDate: b
                })
                .then(res => {
                  this.formData.processingTaskList[
                    this.gongyiIndex
                  ].taskCraftList.push({
                    ...element,
                    value: res.data.avg,
                    unitName: res.data.unit
                  });
                });
            } else {
              this.formData.processingTaskList[
                this.gongyiIndex
              ].taskCraftList.push(element);
            }
          });
        } else {
          this.formData.processingTaskList[this.gongyiIndex].taskCraftList.push(
            ...e
          );
        }
      }
      if (this.processBatchType === "委托加工") {
        if (this.formData.startTime && this.formData.endTime) {
          e.forEach(element => {
            if (element.dataItemId) {
              let a = this.formData.startTime.replace(/-/g, "/");
              let b = this.formData.endTime.replace(/-/g, "/");
              productionLineApi
                .getAvgByDataItemIdAndDate({
                  dataItemId: element.dataItemId,
                  startDate: a,
                  endDate: b
                })
                .then(res => {
                  this.formData.processingTaskList[
                    this.gongyiIndex
                  ].taskCraftList.push({
                    ...element,
                    value: res.data.avg,
                    unitName: res.data.unit
                  });
                });
            } else {
              this.formData.processingTaskList[
                this.gongyiIndex
              ].taskCraftList.push(element);
            }
          });
        } else {
          this.formData.processingTaskList[this.gongyiIndex].taskCraftList.push(
            ...e
          );
        }
      }
    },
    getUnit(e) {
      if (this.processBatchType === "委托加工") {
        this.UnitOptions.forEach(item => {
          if (e === item.unitNo) {
            this.formData.rawMaterialUnitName = item.unitName;
            this.formData.outputUnitNo = item.unitNo;
            this.formData.outputUnitName = item.unitName;
          }
        });
      }
    },
    // 获取单位选择项
    getUnitOptions() {
      UnitApi.queryUnit().then(res => {
        if (res.code === 1) {
          this.UnitOptions = res.data;
        }
      });
    },
    // 删除工艺参数
    delTaskgy(idx, index) {
      this.formData.processingTaskList[idx].taskCraftList.splice(index, 1);
    },
    // 添加工艺参数
    addTaskgy(idx, e) {
      this.gongyiIndex = idx;
      this.productionProcessesNo = e;
      this.addProcessDialog = true;
    },
    rawMaterialChange(item) {
      this.$set(this.formData, "materialBatchNo", undefined);
      this.$emit("rawMaterialChange", item);
      this.$nextTick(() => {
        this.$refs["ruleForm"].clearValidate("materialBatchNo");
      });
    },
    materialBatchChange(e) {
      this.rawMaterialBatchOptions.forEach(item => {
        if (item.rawMaterialBatchNo === e) {
          this.inputRawMaterialQuantity = `请输入原料数量(已存数量${item.remainingQuantity})`;
          // this.$set(
          //   this.formData,
          //   "rawMaterialQuantity",
          //   item.remainingQuantity
          // );
          this.$set(this.formData, "rawMaterialUnitNo", item.unitNo);
          this.formData.rawMaterialUnitName = item.unitName;
        }
      });
    },
    del(index) {
      // if (this.formData.processingTaskList.length === 1) {
      //   return;
      // } else {
      //   this.formData.processingTaskList.splice(index, 1);
      // }
      this.formData.processingTaskList.splice(index, 1);
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          console.log(this.formData);
          this.$emit("addEvent", this.formData);
          this.cancel();
        }
      });
    },
    closeAddProcessDialog() {
      this.addProcessDialog = false;
    },
    cancel() {
      this.$emit("closeDialog");
      this.formData = {};
      //  使用clearValidate移除表单验证结果
      this.$nextTick(() => {
        this.$refs["ruleForm"].clearValidate();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__label {
  min-width: 160px !important;
}
.button {
  display: flex;
  justify-content: space-between;
}
.allGongyi {
  display: flex;
  flex-wrap: wrap;
  .gongyi {
    display: flex;
    width: 96%;
    margin-right: 0px;
    .del {
      margin-left: 0px;
    }
  }
}
.addP {
  margin-left: 24%;
}
.rawMaterialQuantity {
  display: flex;
}
</style>
